package com.qy.controller.app;

import com.qy.base.core.*;
import com.qy.model.Member;
import com.qy.model.Policy;
import com.qy.model.Toll;
import com.qy.model.Unit;
import com.qy.service.*;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zaq on 2018/07/10.
 */
@RestController
@RequestMapping("app/unit")
public class UnitController {
    @Resource
    private UnitService unitService;

    @Resource
    private TollService tollService;

    @Resource
    private PolicyOrderService policyOrderService;

    @Resource
    private EnshrineService enshrineService;

    @Resource
    private MemberService memberService;

    @PostMapping("/add")
    public Result add(@RequestBody Unit unit) {
        unitService.save(unit);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        unitService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Unit unit) {
        unitService.update(unit);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Unit unit = unitService.findById(id);
        return ResultGenerator.successResult(unit);
    }

    @GetMapping("/list")
    public Result list(PageBean<Unit> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Unit> list = unitService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //精选单位
    @GetMapping("/selectedUnit")
    public Result selectedUnit(PageBean<Unit> page, Unit unit) {
//        PageHelper.startPage(page.getPageNum(), 3, "brower_number desc");
        PageHelper.startPage(page.getPageNum(), 3, "add_time desc");
        List<Unit> list = unitService.selectAllUnit(unit);
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //单位列表
    @GetMapping("/unitList")
    public Result unitList(PageBean<Unit> page, Unit unit) {
        PageHelper.startPage(page.getPageNum(), page.getSize(), "add_time desc");
        List<Unit> list = unitService.selectAllUnit(unit);
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //单位详情
    @GetMapping("/unitDetail")
    public Result unitDetail(Unit unit) {
        if (unit.getId() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        List<Unit> list = unitService.selectUnitById(unit);
        Integer visibleState = 1;//1不可见 2可见
        Integer enshrineState = 1;//1未收藏 2已收藏
        Integer registerState = 1;//1未注册 2已注册
        Map<String, Object> map = new HashMap<>();
        map.put("visibleState", visibleState);
        map.put("enshrineState", enshrineState);
        map.put("registerState", registerState);
        map.put("unit_surplus_number", 0);
        map.put("member_surplus_number", 0);

        Toll toll = tollService.selectOnePolicyPrice();
        map.put("onePolicyPrice", toll.getAmount());

        if (unit.getUser_id() != null) {
            //判断是否可见
            visibleState = policyOrderService.selectVisibleState(unit.getUser_id(), unit.getId(), 2);
            //判断是否收藏
            enshrineState = enshrineService.selectEnshrineState(unit.getUser_id(), unit.getId(), 2);

            Member member = memberService.findById(unit.getUser_id());

            //判断是否注册
            if (member.getPhone() == null || member.getPhone().isEmpty()) {
                registerState = 1;
            } else {
                registerState = 2;
            }

            map.put("visibleState", visibleState);
            map.put("enshrineState", enshrineState);
            map.put("registerState", registerState);
            map.put("unit_surplus_number", member.getUnit_surplus_number());
            map.put("member_surplus_number", member.getMember_surplus_number());
        }
        return ResultGenerator.successResult(list, map);
    }

}
